<?php

/**
 * 数据签名认证
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function data_auth_sign($data,$screct) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data,$screct); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}

/**
 * @param  [type]
 * @param  [type]
 * @return [type]
 */
function get_kh_sign($app_key,$app_screct){


	$timestamp = time(); 
	$str = $app_key.",".$timestamp.",".$app_screct; 

	$sign = hash('sha256', $str);



    return $sign;
}


function http_get($url,$token){

        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Authorization:'.$token)
		 ); 
        $result = curl_exec($ch);  
        curl_close($ch);  
        return $result;
 }



/**
 * curl post json 连接
 * @param  [type]
 * @param  [type]
 * @return [type]
 */
function http_post_json($url,$value,$token){


  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  curl_setopt($ch, CURLOPT_POSTFIELDS, $value);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($value),
    'Authorization:'.$token)
 );

 $info = curl_exec($ch);


   if (curl_errno($ch)) {
    echo 'Errno'.curl_error($ch);
  }
  curl_close($ch);
  return $info;

}


function http_get_json($url,$value,$token){


  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  curl_setopt($ch, CURLOPT_POSTFIELDS, $value);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($value),
    'Authorization:'.$token)
 );

 $info = curl_exec($ch);


   if (curl_errno($ch)) {
    echo 'Errno'.curl_error($ch);
  }
  curl_close($ch);
  return $info;

}






//上传文件黑名单过滤
function upload_replace($str){
	$farr = ["/php|php3|php4|php5|phtml|pht|/is"];
	$str = preg_replace($farr,'',$str);
	return $str;
}



//根据键名获取键值
function getItemVal($val,$item_config){
	if(!is_null($val)){
		$str = '';
		foreach(explode(',',$val) as $v){
			foreach(json_decode($item_config,true) as $m){
				if($v == $m['val']){
					$str .= $m['key'].',';
				}
			}
		}
		return rtrim($str,',');
	}
}

//根据键值获取键名
function getValByKey($val,$item_config){
	if($val){
		$str = '';
		foreach(explode(',',$val) as $v){
			foreach(json_decode($item_config,true) as $m){
				if($v == $m['key']){
					$str .= $m['val'].',';
				}
			}
		}
		return rtrim($str,',');
	}
}

//无限极分类转为带有 children的树形select结构
function _generateSelectTree ($data, $pid = 0) {
	$tree = [];
	if ($data && is_array($data)) {
		foreach($data as $v) {
			if($v['pid'] == $pid) {
				$tree[] = [
					'key' => $v['key'],
					'val' => $v['val'],
					'children' => _generateSelectTree($data, $v['val']),
				];
			}
		}
	}
	return $tree;
}

//无限极分类转为带有 children的树形list表格结构
function _generateListTree ($data, $pid = 0,$config=[]) {
	$tree = [];
	if ($data && is_array($data)) {
		foreach($data as $v) {
			if($v[$config[1]] == $pid) {
				$tree[] = array_merge($v,['children' => _generateListTree($data, $v[$config[0]],$config)]);
			}
		}
	}
	return $tree;
}


//获取键值对信息
function getItemData($data){
	$str = in_array(json_encode(array_values($data)),['[]','[[]]']) ? '' : json_encode(array_values($data),JSON_UNESCAPED_UNICODE);
	return $str;
}



/**
 * tp官方数组查询方法废弃，数组转化为现有支持的查询方法
 * @param array $data 原始查询条件
 * @return array
 */
//查询格式化
function formatWhere($query,$where){
	foreach($where as $k=>$v){
		if(is_array($v)){
			if($v[1]){
				switch($v[0]){
					case 'like';
						$query = $query->whereLike($k,'%'.$v[1].'%');
					break;
					
					case 'not like';
						$query = $query->whereNotLike($k,$v[1]);
					break;
					
					case 'in';
						$query = $query->whereIn($k,$v[1]);
					break;
					
					case 'not in';
						$query = $query->whereNotIn($k,$v[1]);
					break;
					
					case 'between';
						$query = $query->whereBetween($k,$v[1]);
					break;
					
					case 'is null';
						$query = $query->whereNull($k);
					break;
					
					case 'not null';
						$query = $query->whereNotNull($k);
					break;
					
					case '<>';
						$query = $query->where($k,'<>',$v[1]);
					break;
					
					case '>';
						$query = $query->where($k,'>',$v[1]);
					break;
					
					case '<';
						$query = $query->where($k,'<',$v[1]);
					break;
					
					case '>=';
						$query = $query->where($k,'>=',$v[1]);
					break;
					
					case '<=';
						$query = $query->where($k,'<=',$v[1]);
					break;
					
					case 'or';
						$query = $query->orWhere($k,$v[2],$v[1]);
					break;
					
					case 'find in set';
						$query = $query->whereFindInSet($k,$v[1]);
					break;
					
					case 'exp';
						$query = $query->whereExp($k,$v[1]);
					break;
				}
			}
		}else{
			if((string)$v != null){
				$query = $query->where($k,$v);
			}
		}
	}
		
	return $query;
}

